package OnlineExam;

import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import OnlineExam.*;
import OnlineExam.db.*;
import registration.*;
import org.apache.struts.action.*;

/**
 *  Sets the attributes for a particular category, done by the category administrator.
 */


public class CategoryAttributesAction extends Action
{

	public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, 
					             HttpServletResponse response)
	{
		HttpSession sess = request.getSession ();

		// check if user has privileges needed
		User user = (User) sess.getAttribute ("USER");
   		Category category = (Category)sess.getAttribute ("CATEGORY");

		if (!user.isTheCategoryAdmin (category.getCategoryId()))
			return mapping.findForward ("failure");

		// at this point, the user has needed privileges
		try 
		{
			Category cat = (Category) form;
			category.setTestLength (cat.getTestLength());
			category.setTimeLimit (cat.getTimeLimit());
			category.setQuestionGeneration (cat.getQuestionGeneration());

			DBUtil.update (category);
				
			return mapping.findForward ("success");
		}
		catch (DBException e) 
		{
			System.out.println ("Exception : " + e);
			return mapping.findForward ("dbError");
		}
	}
}
